﻿using System;
using System.Globalization;
using System.IO;
using System.Text;

namespace Jodell.Lua
{
    public class Lua_CsvWriter:IDisposable
    {
        private readonly string path;

        public Lua_CsvWriter(string path)
        {
            this.path = path;
            AutoFlush = true;
        }
        private StreamWriter writer = null;
        public bool IsOpen
        {
            get
            {
                return writer != null;
            }
        }
        public void Open()
        {
            if (IsOpen)
            {
                return;
            }
            
            writer = new StreamWriter(path, false, Encoding.UTF8);
        }
        public void Close()
        {
            if (IsOpen)
            {
                writer.Flush();
                writer.Close();
                writer = null;
            }
        }

        public bool AutoFlush { get; set; }

        public void WriteString(string str)
        {
            writer.Write(str);
            if (AutoFlush)
            {
                writer.Flush();
            }
        }
        public void WriteFloat(byte[] datas, int offset)
        {
            var d = Lua_Converter.ToFloat(datas, offset);
            writer.Write(d);
            if (AutoFlush)
            {
                writer.Flush();
            }
        }
        public void WriteDouble(byte[] datas, int offset)
        {
            var d = Lua_Converter.ToDouble(datas, offset);
            writer.Write(d);
            if (AutoFlush)
            {
                writer.Flush();
            }
        }
        public void WriteUInt16(byte[] datas, int offset)
        {
            var d = Lua_Converter.ToUInt16(datas, offset);
            writer.Write(d);
            if (AutoFlush)
            {
                writer.Flush();
            }
        }
        public void WriteInt16(byte[] datas, int offset)
        {
            var d = Lua_Converter.ToInt16(datas, offset);
            writer.Write(d);
            if (AutoFlush)
            {
                writer.Flush();
            }
        }
        public void WriteUInt32(byte[] datas, int offset)
        {
            var d = Lua_Converter.ToUInt32(datas, offset);
            writer.Write(d);
            if (AutoFlush)
            {
                writer.Flush();
            }
        }
        public void WriteInt32(byte[] datas, int offset)
        {
            var d = Lua_Converter.ToInt32(datas, offset);
            writer.Write(d);
            if (AutoFlush)
            {
                writer.Flush();
            }
        }
        public void WriteUTF8String(byte[] datas, int offset, int count)
        {
            var s = Lua_Converter.ToString(datas, offset, count);
            writer.Write(s);
            if (AutoFlush)
            {
                writer.Flush();
            }
        }
        public void WriteHexString(byte[] datas, int offset, int count)
        {
            var s = Lua_Converter.ToHexString(datas, offset, count);
            writer.Write(s);
            if (AutoFlush)
            {
                writer.Flush();
            }
        }
        public void WriteSplitter()
        {
            writer.Write(CultureInfo.CurrentCulture.TextInfo.ListSeparator);
            if (AutoFlush)
            {
                writer.Flush();
            }
        }
        public void WriteFloat(double value)
        {
            writer.Write((float)value);
            if (AutoFlush)
            {
                writer.Flush();
            }
        }
        public void WriteFloat(float value)
        {
            writer.Write(value);
            if (AutoFlush)
            {
                writer.Flush();
            }
        }
        public void WriteDouble(double value)
        {
            writer.Write(value);
            if (AutoFlush)
            {
                writer.Flush();
            }
        }
        public void WriteUInt16(UInt16 value)
        {
            writer.Write(value);
            if (AutoFlush)
            {
                writer.Flush();
            }
        }
        public void WriteInt16(Int16 value)
        {
            writer.Write(value);
            if (AutoFlush)
            {
                writer.Flush();
            }
        }
        public void WriteUInt32(UInt32 value)
        {
            writer.Write(value);
            if (AutoFlush)
            {
                writer.Flush();
            }
        }
        public void WriteInt32(Int32 value)
        {
            writer.Write(value);
            if (AutoFlush)
            {
                writer.Flush();
            }
        }

        public void WriteLine()
        {
            writer.WriteLine();
            if (AutoFlush)
            {
                writer.Flush();
            }
        }

        public void Dispose()
        {
            writer?.Flush();
            writer?.Close();
            writer?.Dispose();
        }
    }
}
